<?php
/*IMPORTANT: 注意检查文件字符集是否为UTF-8*/
function ZabbixFuncSignin($id)
{
	include ('./config/zabbix.conf.secret.php');
	$sock = new sockstuff;
	$sock->setHttpSock($zabbixConfig[$id]['host'],$zabbixConfig[$id]['port'],"post",$zabbixConfig[$id]['path']."index.php?login=1",null,$zabbixConfig[$id]['protocol']);
	$return=$sock->httpSock("form_refresh=1&form=1&name=".$zabbixConfig[$id]['username']."&password=".$zabbixConfig[$id]['password']."&enter=Enter");
	ZabbixFuncWriteCookie($id,$return);
	return $return;
}
function ZabbixFuncWriteCookie($id,$httpResponseStr)
{
	include ('./config/zabbix.conf.secret.php');
	preg_match_all ("/Set-Cookie:[^(\r\n)]*/", $httpResponseStr, $matches);
	$cookie = "";
	if(@$matches[0])
	{
		$cookie = $matches[0][count($matches[0])-1];
	}
	$cookie = str_replace("Set-Cookie: ","",$cookie);
	file_put_contents("./cache/".$zabbixConfig[$id]['prefix']."zabbix.cookie.cache",$cookie);
}
function ZabbixFuncGetDashboard($id=0,$var=0)
{
	if($var>3)
	{
		die("Error: Zabbix Unauthorized.");
	}
	include ('./config/zabbix.conf.secret.php');
	$arrReturn = array();
	$sock = new sockstuff;
	if ($id==0)
	{
		for ($i=0;$i<count($zabbixConfig);++$i)
		{
			$cookie = file_get_contents("./cache/".$zabbixConfig[$i]['prefix']."zabbix.cookie.cache");
			$sid = file_get_contents("./cache/".$zabbixConfig[$i]['prefix']."zabbix.sid.cache");
			$sock->setHttpSock($zabbixConfig[$i]['host'],$zabbixConfig[$i]['port'],"post",$zabbixConfig[$i]['path']."dashboard.php?output=html&{$sid}&upd_counter=0&pmasterid=mainpage",$cookie,$zabbixConfig[$i]['protocol']);
			$return=$sock->httpSock("favobj=hat&favref=hat_syssum&action=refresh");
			preg_match_all("/Operation cannot be performed due to unauthorized request/",$return,$matches);
			preg_match_all("/Login/",$return,$matches1);
			if(!@$matches[0]&&!@$matches1[0])
			{
				array_push($arrReturn,array("prefix"=>$zabbixConfig[$i]['prefix'],"data"=>ZabbixFuncDashboardParse($return)));
			}
			else
			{
				ZabbixFuncSignin($i);
				$cookie = file_get_contents("./cache/".$zabbixConfig[$i]['prefix']."zabbix.cookie.cache");
				ZabbixFuncGetSid($i,$cookie);
				array_push($arrReturn,array("prefix"=>$zabbixConfig[$i]['prefix'],"data"=>(ZabbixFuncGetDashboard($i,++$var))));
			}
		}
		return $arrReturn;
	}
	else
	{
		$cookie = file_get_contents("./cache/".$zabbixConfig[$id]['prefix']."zabbix.cookie.cache");
		$sid = file_get_contents("./cache/".$zabbixConfig[$id]['prefix']."zabbix.sid.cache");
		$sock->setHttpSock($zabbixConfig[$id]['host'],$zabbixConfig[$id]['port'],"post",$zabbixConfig[$id]['path']."dashboard.php?output=html&{$sid}&upd_counter=0&pmasterid=mainpage",$cookie,$zabbixConfig[$id]['protocol']);
		$return=$sock->httpSock("favobj=hat&favref=hat_syssum&action=refresh");
		preg_match_all("/Operation cannot be performed due to unauthorized request/",$return,$matches);
		preg_match_all("/Login/",$return,$matches1);
		if(!@$matches[0]&&!@$matches1[0])
		{
			return ZabbixFuncDashboardParse($return);
		}
		else
		{
			//echo $return;
			ZabbixFuncSignin($id);
			$cookie = file_get_contents("./cache/".$zabbixConfig[$id]['prefix']."zabbix.cookie.cache");
			ZabbixFuncGetSid($id,$cookie);
			$return = ZabbixFuncGetDashboard($id,++$var);
			return $return;
		}
	}
}
function ZabbixFuncGetSid($id,$cookie)
{
	include ('./config/zabbix.conf.secret.php');
	$sock = new sockstuff;
	$sock->setHttpSock($zabbixConfig[$id]['host'],$zabbixConfig[$id]['port'],"get",$zabbixConfig[$id]['path']."index.php",$cookie,$zabbixConfig[$id]['protocol']);
	$return=$sock->httpSock("");
	preg_match_all ("/sid=[a-zA-Z0-9]*/", $return, $matches);
	file_put_contents("./cache/".$zabbixConfig[$id]['prefix']."zabbix.sid.cache",$matches[0][0]);
	return $matches[0][0];//返回sid=xxxxxx
}
function ZabbixFuncDashboardParse($html)
{
	$html=htmlspecialchars_decode($html);
	preg_match_all ("/hintBox.showOver\(event,this,\'[^\']*/",$html,$match);
	preg_match_all ("/<table[\S\s]*/",@$match[0][0],$match1);
	preg_match_all ("/eventid=[\d]*/",@$match1[0][0],$match2);
	$arrEventIds = array();
	foreach (@$match2[0] as $eventids)
	{
		$tmp = null;
		preg_match_all ("/[0-9]+/",$eventids,$tmp);
		array_push($arrEventIds,$tmp[0][0]);
	}
	return (array("eventids"=>$arrEventIds,"events"=>ZabbixFuncInfoSplit(preg_replace("/(>)+/","#|#",preg_replace("/<[^(>)]*/","",@$match1[0][0])))));
}
function ZabbixFuncInfoSplit($info)
{
	$arrInfo = array();
	$token = strtok($info,"#|#");
	while (@$token)
	{
		array_push($arrInfo,$token);
		$token = strtok("#|#");
	}
	$arrInfo2 = array();
	$title = array(0=>"host",1=>"Issue",2=>"Age",3=>"Ack",4=>"Actions");
	array_push($arrInfo2,$title);
	$row = array();
	foreach($arrInfo as $element)
	{
		array_push($row,$element);
		if(count($row)==count($title))
		{
			if($title==$row)
			{
				;
			}
			else
			{
				array_push($arrInfo2,$row);
			}
			$row = array();
		}
	}
	array_shift($arrInfo2);
	array_shift($arrInfo2);
	return $arrInfo2;
}
?>